package com.example.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.myapplication.R;
import com.example.myapplication.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends BaseActivity {

    private AppCompatEditText zhanghao;
    private AppCompatEditText mima;
    private AppCompatEditText nicheng;
    private AppCompatTextView zhuceView;
    private View zhanghaoView,mimaView,nichengView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);
        zhuceView.setOnClickListener(this::onClick);
    }

    @Override
    public void initView() {
        zhanghao = findViewById(R.id.zhanghao);
        mima = findViewById(R.id.mima);
        zhanghaoView = findViewById(R.id.zhanghaoView);
        mimaView = findViewById(R.id.mimaView);
        nichengView = findViewById(R.id.nichengView);
        nicheng = findViewById(R.id.nicheng);
        zhuceView = findViewById(R.id.zhuceView);

    }

    @Override
    public void initEvent() {

        zhanghao.setOnFocusChangeListener((view,b)->{
            if(b){
                zhanghaoView.setBackgroundResource(R.color.denglujian);
                mimaView.setBackgroundResource(R.color.textHint);
                nichengView.setBackgroundResource(R.color.textHint);
            }
        });
        mima.setOnFocusChangeListener((view,b)->{
            if(b){
                zhanghaoView.setBackgroundResource(R.color.textHint);
                mimaView.setBackgroundResource(R.color.denglujian);
                nichengView.setBackgroundResource(R.color.textHint);
            }
        });
        nicheng.setOnFocusChangeListener((view,b)->{
            if(b){
                zhanghaoView.setBackgroundResource(R.color.textHint);
                mimaView.setBackgroundResource(R.color.textHint);
                nichengView.setBackgroundResource(R.color.denglujian);
            }
        });
    }

    private void onClick(View view) {
        String mobile = zhanghao.getText().toString();
        String password = mima.getText().toString();
        String nickname = nicheng.getText().toString();
        if (mobile.isEmpty()) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
        }
        if (password.isEmpty()) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
        }
        if (nickname.isEmpty()) {
            Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
        }
        zhuceView.setEnabled(false);
        zhuceView.setText("正在注册...");
        String url = "http://192.168.8.61/api/base/register?mobile=" + mobile + "&password=" + password + "&nickname=" + nickname;
        OkGo.<String>get(url).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                System.out.println(response.body());
                try {
                    JSONObject jsonObject = new JSONObject(response.body());
                    int code = jsonObject.getInt("code");
                    String msg = jsonObject.getString("msg");
                    if (code == 1) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent();
                        intent.putExtra("zhanghao", mobile);
                        setResult(999,intent);
                        finish();
                        return;
                    }
                    Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                zhuceView.setEnabled(true);
                zhuceView.setText("注册");
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                System.out.println(response.getException().toString());
                zhuceView.setEnabled(true);
                zhuceView.setText("注册");
            }
        });
    }
}
